package com.caseProcess.controller;

import com.caseProcess.entity.Result;
import com.caseProcess.entity.VCaseAskForList;
import com.caseProcess.entity.VCaseProcInfo;
import com.caseProcess.service.VCaseAskForListService;
import com.caseProcess.service.VCaseProcInfosService;
import com.caseProcess.utils.PagingUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author kittlen
 * @version 1.0
 * @date 2020年11月15日 14点41分
 */
@RestController
@RequestMapping("/vCaseAskForList")
public class VCaseAskForListController {
    @Resource
    VCaseAskForListService vCaseAskForListService;

    /**
     *
     * @param caseId 警情id
     * @param processId 分派id
     * @param procTatus 警情过程id -1查询全部 99查询 审批与审核的
     * @param askForPoliceId 分派者id
     * @param procDepartmentId 处理部门
     * @param procPoliceId 处理警员
     * @param conProcPoliceId 当前节点处理人id
     * @param conNextPoliceId 当前节点下一个处理人id
     * @param procLevel 督办级别
     * @param caseDescription 警情描述
     * @param isUrging 是否催办
     * @param isBack 是否回退
     * @param isShowEnd 是否显示办结的
     * @return
     */
    @RequestMapping("/selects")
    public Result selects(@RequestParam(required = false,defaultValue = "")String caseId,
                          @RequestParam(required = false,defaultValue = "")String processId,
                          @RequestParam(required = false,defaultValue = "-1") Long procTatus,
                          @RequestParam(required = false,defaultValue = "")String askForPoliceId,
                          @RequestParam(required = false,defaultValue = "")String procDepartmentId,
                          @RequestParam(required = false,defaultValue = "")String procPoliceId,
                          @RequestParam(required = false,defaultValue = "")String conProcPoliceId,
                          @RequestParam(required = false,defaultValue = "")String conNextPoliceId,
                          @RequestParam(required = false,defaultValue = "")String procLevel,
                          @RequestParam(required = false,defaultValue = "")String caseDescription,
                          @RequestParam(required = false,defaultValue = "0")int page,
                          @RequestParam(required = false,defaultValue = "0")int limit,
                          @RequestParam(required = false,defaultValue = "false") boolean isUrging,
                          @RequestParam(required = false,defaultValue = "false") boolean isBack,
                          @RequestParam(required = false,defaultValue = "false") boolean isShowEnd,
                          @RequestParam(required = false,defaultValue = "")String startTime,
                          @RequestParam(required = false,defaultValue = "")String endTime){
        VCaseAskForList vCaseAskForList=new VCaseAskForList();
        vCaseAskForList.setProcessId(processId);
        if(procTatus!=-1){
            vCaseAskForList.setProcTatus(procTatus);
        }else{
            vCaseAskForList.setProcTatus(-99);
        }
        if(procTatus==99){
            vCaseAskForList.setProcTatus(3);
        }
        vCaseAskForList.setAskForPoliceId(askForPoliceId);
        vCaseAskForList.setProcDepartmentId(procDepartmentId);
        vCaseAskForList.setProcPoliceId(procPoliceId);
        vCaseAskForList.setConProcPoliceId(conProcPoliceId);
        vCaseAskForList.setConNextPoliceId(conNextPoliceId);
        vCaseAskForList.setUrging(isUrging);
        vCaseAskForList.setBack(isBack);
        vCaseAskForList.setShowEnd(isShowEnd);
        vCaseAskForList.setCaseId(caseId);
        vCaseAskForList.setProcLevel(procLevel);
        vCaseAskForList.setCaseDescription(caseDescription);
        List<VCaseAskForList> lists=new ArrayList<VCaseAskForList>();
        if(!"".equals(startTime)||!"".equals(endTime)){
            lists=vCaseAskForListService.selectVCaseAskForListsOrTime(vCaseAskForList,startTime,endTime);
            if(procTatus==99){
                vCaseAskForList.setProcTatus(4);
                List<VCaseAskForList> lists4=vCaseAskForListService.selectVCaseAskForListsOrTime(vCaseAskForList,startTime,endTime);
                for(int i=0;i<lists4.size();i++){
                    lists.add(lists4.get(i));
                }
            }
        }else {
            lists = vCaseAskForListService.selectVCaseAskForLists(vCaseAskForList);
            if (procTatus == 99) {
                vCaseAskForList.setProcTatus(4);
                List<VCaseAskForList> lists4 = vCaseAskForListService.selectVCaseAskForLists(vCaseAskForList);
                for (int i = 0; i < lists4.size(); i++) {
                    lists.add(lists4.get(i));
                }
            }
        }
        Map<String,VCaseAskForList> map=new LinkedHashMap<String, VCaseAskForList>();
        for(int i=0;i<lists.size();i++){
            map.put(lists.get(i).getProcessId(),lists.get(i));//去除回退重复数据
        }
        lists.clear();
        for(String key:map.keySet()){
            lists.add(map.get(key));
        }
        Result result = new Result();
        result.setCount(lists.size());
        if(page!=0&&limit!=0){
            lists= PagingUtil.paging(lists,page,limit);
        }

        if (lists.size() >= 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(lists);
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }
}
